<?php
error_reporting(0);
header("content-Type: text/html; charset=utf-8");//输出编码
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta name="viewport" content="width=device-width,minimum-scale=1.0,maximum-scale=1.0"/>
<meta name="apple-mobile-web-app-capable" content="yes" />
<title>我是淘宝优惠券-youhui.17386.net</title>
<meta name="author" content="yujianyue, admin@ewuyi.net">
<meta name="copyright" content="youhui.17386.net">
<!--Powered By youhui.17386.net-->
<head>
<body>
<style type="text/css">
/*结果*/
*{FONT-SIZE:14px;margin:0 auto;font-family:"microsoft yahei";line-height:150%;}
a{color:blue;text-decoration:none;}
s{color:#999999;}
b{color:red;}
a:hover {color:green;}
table{border-top:0px solid #0180CF; margin:0 auto;width:95%;max-width:568px;}
table td{padding:3px;word-wrap:break-word;word-break:break-all;min-width:80px;max-width:300px;}
.tt{background:#e5f2fa;line-height:18px;FONT-SIZE:18px;}
.tup{text-align:center;vertical-align:center;width:100px;height:100px;}
.img{width:96%;min-width:88px;max-width:128px;border:0px;}
.tim{font-family:"Microsoft JhengHei";}
.jia{height:28px;color:red;}
.but{width:68px;}
</style>

<?php
function characet($data){
  if(!empty($data) ){    
    $fileType = mb_detect_encoding($data , array('UTF-8','GBK','LATIN1','BIG5')) ;   
    if( $fileType != 'UTF-8'){   
      $data = mb_convert_encoding($data ,'UTF-8' , $fileType);   
    }   
  }   
  return $data;    
}

function get_file_line($file_name, $line){
  $n = 0;
  $handle = fopen($file_name,'r');
  if ($handle) {
    while (!feof($handle)){
        ++$n;
       $out = fgets($handle, 4096);
       if($n==$line) break;
    }
    fclose($handle);
  }
  if($line==$n) return $out;
  return false;
}

$moban01="<table class='tbyouhui'>"
  . "<tbody>"
  . " <tr>"
  . "  <td rowspan=\"2\" class='tup' ><a href='<!--商品主图-->' target='_blank'>\r\n"
  . "   <img src='<!--商品主图-->' class='img' align='absmiddle'></a></td>\r\n"
  . "   <td colspan=\"2\" class='tim' style=\"text-align:left;vertical-align:top;\">\r\n"
  . "   <a href='<!--淘宝客链接-->' target='_blank'><!--商品名称--></a></td>\r\n"
  . " </tr>\r\n"
  . " <tr>\r\n"
  . "  <td class='jia'><s>&nbsp;<!--商品价格(单位：元)-->&nbsp;</s><br><!--优惠券面额--></td>\r\n"
  . "  <td class='but'><a href='<!--商品优惠券推广链接-->' target='_blank'>先领优惠券</a></td>\r\n"
  . " </tr>\r\n"
  . "</tbody>\r\n"
  . "</table>\r\n\r\n";

//error_reporting(0);
$stime=microtime(true); 

$linet = "3"; //随机显示的宝贝数量/条数推荐：1-10
$files = "taobao.dat"; //数据库名称：数字字母组成：文本内容可以是编码gb2312/utf-8

if(!file_exists($files)){
$files = "taobao.dat"; //默认备用数据库：同级taobao.dat
echo "<!-- Powered By youhui.17386.net 文件 $files 不存在 -->\r\n";
}else{
  $a=file($files);//把文件的所有内容获取到数组里面
  $n=count($a);//获得总行数
echo "<!-- Powered By youhui.17386.net 文件: $files $n 条记录 -->\r\n";
$rnd_tite = trim(characet(get_file_line($files, "1")));//读取列标题
$rnd_tito=explode("\t",$rnd_tite);
for($line=0; $line<$linet; $line++) {
$lines = rand(2, $n-1); //随机读取栏目
echo "<!-- Powered By youhui.17386.net 第 $lines 条记录 -->\r\n";
$rnd_ones = characet(get_file_line($files, $lines));
$rnd_ones = trim($rnd_ones);
$tab_one=explode("\t",$rnd_ones);
       $iit = -1;
$html_ones = $moban01;
   foreach($tab_one as $tabu){
       $iit++;
       $taba=$rnd_tito[$iit];
$html_ones = str_replace("<!--$taba-->",$tabu,$html_ones); 
   }
echo $html_ones;
}
}
$etime=microtime(true);//获取程序执行结束的时间
$total=$etime-$stime;   //计算差值
echo "<!--页面执行时间：{$total} ]秒-->";
?>
</body>
</html>